package com.cricked.parser;
/**
 * @author aizaz.haider
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.http.client.CircularRedirectException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.cricked.parser.utils.Util;

import android.os.AsyncTask;

public class _Parser {
	// ===========================================================
	// Constants
	// ===========================================================
	private final PublishResultHelper mPublishHelper = PublishResultHelper.getInstance();
	private final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17";

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================
	public void parseCricketFeed(String pURL) {
		HTMLParserTask task = new HTMLParserTask();
		task.execute(pURL);
		// task.execute("http://www.espncricinfo.com/ci/engine/current/match/516858.html?view=live;wrappertype=live");
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	private class HTMLParserTask extends AsyncTask<String, Void, List<CricketFeedInfo>> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@SuppressWarnings("unused")
		@Override
		protected List<CricketFeedInfo> doInBackground(String... params) {
			List<CricketFeedInfo> listFeeds = null;
			CricketFeedInfo cricketInfo = new CricketFeedInfo();
			try {

				Connection con = Jsoup.connect(params[0]).ignoreContentType(true).userAgent(USER_AGENT).timeout(1000 * 5);
				Document doc = con.get();
				Elements elements = doc.select("iframe#live_iframe");
				doc = Jsoup.connect(elements.attr("abs:src")).ignoreContentType(true).userAgent(USER_AGENT).get();

				// ===Left Div
				Elements leftDiv = doc.select("div.headLeftDiv").first().children();
				cricketInfo.leftDiv = getDivElements(leftDiv);

				// ===Right Div
				Elements rightDiv = doc.select("div.headRightDiv").select("ul").first().children();
				cricketInfo.rightDiv = getDivElements(rightDiv);

				// ===Score Table
				elements = doc.select("table.mainScoresTable").first().children().first().children();

				// ---------------------------------------------------------------------------------
				{
					Elements heading1 = elements.get(2).children().select("td:not([colspan]):has(*)");
					cricketInfo.tableRow1Heading = getDivElements(heading1);
					heading1 = null;
					System.gc();

					Elements values1 = elements.get(3).children().select("td:not([colspan])");
					cricketInfo.tableRow1Values1 = Util.clean(getDivElements(values1));
					values1 = null;
					System.gc();

					Elements values2 = elements.get(4).children().select("td:not([colspan])");
					cricketInfo.tableRow1Values2 = Util.clean(getDivElements(values2));
					values2 = null;
					System.gc();
				}
				// ---------------------------------------------------------------------------------
				{
					Elements heading2 = elements.get(6).children().select("td:not([colspan]):has(*)");
					cricketInfo.tableRow2Heading = getDivElements(heading2);
					heading2 = null;
					System.gc();

					Elements values1 = elements.get(7).children().select("td:not([colspan])");
					cricketInfo.tableRow2Values1 = Util.clean(getDivElements(values1));
					values1 = null;
					System.gc();

					Elements values2 = elements.get(8).children().select("td:not([colspan])");
					cricketInfo.tableRow2Values2 = Util.clean(getDivElements(values2));
					values2 = null;
					System.gc();

				}
				// ---------------------------------------------------------------------------------
				Elements otherInfo = doc.select("p.liveDetailsText");
				cricketInfo.otherInfo = Util.clean(getDivElements(otherInfo));
				otherInfo = null;
				System.gc();

			} catch (Exception ex) {
				ex.printStackTrace();
			}
			List<CricketFeedInfo> list = new ArrayList<CricketFeedInfo>();
			list.add(cricketInfo);
			return list;
		}

		@Override
		protected void onPostExecute(List<CricketFeedInfo> result) {
			super.onPostExecute(result);
			if (result == null)
				mPublishHelper.publishResule(Collections.<CricketFeedInfo> emptyList());
			else
				mPublishHelper.publishResule(result);
		}

	}

	private String[] getDivElements(Elements pElements) {
		int j = 0;
		String[] pArray = new String[pElements.size()];
		for (int i = 0; i < pElements.size(); i++) {
			String value = pElements.get(i).text();
			if (!value.equals("") && !value.equals(" ")) {
				pArray[j] = pElements.get(i).text();
				j++;
			}

		}
		return pArray;
	}
}
